package com.tengju.bff.interfaces.shared.render;

import com.tengju.bff.interfaces.shared.AmountVO;
import com.tengju.data.domain.model.analysis.Meta;
import com.tengju.data.domain.model.analysis.Row;
import com.tengju.data.domain.shared.CastUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author chenhang
 * @Date 2021/7/8
 */
@Data
public class ChartVO {

    @ApiModelProperty(value = "图示标题")
    private String title;

    @ApiModelProperty(value = "x轴数据")
    private List<String> xAxisdata;

    @ApiModelProperty(value = "曲线名称")
    private Legend legend;

    @ApiModelProperty(value = "曲线数据")
    private List<Series> series;

    public ChartVO(String title) {
        this.title = title;
    }

    @Data
    @AllArgsConstructor
    static class Legend {
        List<String> data;

        public void addData(String singleData) {
            if (CollectionUtils.isEmpty(data)) {
                data = new ArrayList<>();
            }
            data.add(singleData);
        }
    }

    @Data
    public static class Series {
        private String type;
        private String name;
        private String stack;
        private List<Object> data;

    }

    public ChartVO addLegend(List<String> data) {
        this.setLegend(new Legend(data));
        return this;
    }

    public ChartVO addLegend(String data) {
        if (legend == null) {
            legend = new Legend(new ArrayList<>());
        }
        legend.addData(data);
        return this;
    }

    public ChartVO buildXAxis(Collection<Row> rows, String xAxisKey) {
        if (CollectionUtils.isEmpty(rows)) {
            return this;
        }
        List<String> xAxisList = rows.stream()
                .map(v -> v.getValue(xAxisKey))
                .map(CastUtil::castString)
                .collect(Collectors.toList());
        this.setXAxisdata(xAxisList);
        return this;
    }

    public ChartVO buildSeries(Collection<Row> rows, Collection<Meta> metas) {
        for (Meta meta : metas) {
            String chartType = meta.getChartType();
            String showName = meta.getShowName();
            List<Object> data = rows.stream()
                    .map(v -> {
                        AmountVO amount = MetaUtil.getAmount(meta, v, metas);
                        if (amount == null) {
                            return null;
                        }
                        return amount.getValue();
                    })
                    .collect(Collectors.toList());
            Series seriesTmp = new Series();
            seriesTmp.setName(showName);
            seriesTmp.setType(chartType);
            seriesTmp.setData(data);
            addSeries(seriesTmp);
        }
        return this;
    }

    public ChartVO buildSeries(List<Object> data, Collection<Meta> metas) {
        for (Meta meta : metas) {
            String chartType = meta.getChartType();
            String showName = meta.getShowName();
            Series seriesTmp = new Series();
            seriesTmp.setName(showName);
            seriesTmp.setType(chartType);
            seriesTmp.setData(data);
            addSeries(seriesTmp);
        }
        return this;
    }

    public void addSeries(Series singleSeries) {
        if (CollectionUtils.isEmpty(series)) {
            series = new ArrayList<>();
        }
        series.add(singleSeries);
    }








}
